<template>
	<view>
		<!-- 状态栏 -->
		<view class="statusBar" :style="'height:'+statusBarHeight+'px'"></view>
		<view class="myheader padding-sm  flex justify-between">
			<view class="font-sub-title">
				<text class="cuIcon-back margin-right-ssm" @click="back()"></text>
				闹钟
			</view>
			<text class="font-sub-title" @click="add()">添加</text>
		</view>
		<view class="cu-list menu">
			<view class="cu-item" v-for="(item,index) in List" @click="goEdit(item.alarmHour,item.alarmMin,item.alarmRepeat)">
				<view class="content">
					{{item.alarmHour<10?'0'+item.alarmHour:item.alarmHour}}:{{item.alarmMin<10?'0'+item.alarmMin:item.alarmMin}}
				</view>
				<view class="action" @click.native.stop>
					<switch class="blue" @change="open($event,item.alarmRepeat,item.alarmHour,item.alarmMin,index)" :class="getRepeat(item.alarmRepeat)?'checked':''" :checked="getRepeat(item.alarmRepeat)"></switch>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const bandSDK = uni.requireNativePlugin('BandSDK')
	var that;
	export default {
		data() {
			return {
				statusBarHeight: Vue.prototype.StatusBar,
				isOpen:false,
				List:[]
			}
		},
		onShow(){
			that = this;
			uni.showLoading({
				
			})
			bandSDK.settingGetAllAlarm(res=>{
				that.List = res.data
				uni.hideLoading()
			})
		},
		methods: {
			back(){
				uni.navigateBack();
			},
			getRepeat(repeat){
				var str = repeat.toString(2)
				var len = str.length
				if(str.length!=8){
					for(var i=0;i<len;i++){
						str = '0' + str;
					}
				}
				if(str[0]=='1'){
					return true;
				}else{
					return false;
				}
			},
			open(e,repeat,hour,min,index){
				var str = repeat.toString(2)
				var len = 8-str.length
				if(str.length!=8){
					for(var i=0;i<len;i++){
						str = '0' + str;
					}
				}
				if(e.target.value){
					str = '1'+str.substring(1)
				}else{
					str = '0'+str.substring(1)
				}
				var newRepeat = parseInt(str,2)
				that.List[index].alarmRepeat=newRepeat
				bandSDK.settingModfiyAlarm({
					newType:0,
					startHour:hour,
					startMin:min,
					newStartHour:hour,
					newStartMin:min,
					newWeekRepeat:newRepeat,
					newDelayTime:0
				},res=>{
					if(res=='success'){
						uni.showToast({
							"title":"保存成功"
						})
						
					}
				})
			},
			goEdit(hour,min,repeat){
				hour = hour<10?'0'+hour:hour
				min =min<10?'0'+min:min
				var time = hour+':'+min
				var isopen = this.getRepeat(repeat)
				uni.navigateTo({
					"url":`/pages/equipment/editalarm?title=编辑&time=${time}&isopen=${isopen}&repeat=${repeat}`
				})
			},
			add(){
				uni.navigateTo({
					"url":"/pages/equipment/editalarm?title=添加&time=08:00&isopen=true&repeat=255"
				})
			}
		}
	}
</script>

<style>

</style>
